﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using DPMP.DataAccess;
using System.Data;

namespace DPMP.ViewModel
{
    public class PatternInstanceViewModel : TreeViewItemViewModel
    {
        #region Private Fields

        // business model of this pattern instancce
        PatternInstance _patternInstance;

        // property command
        ICommand _propertiesCommand;

        // view code command
        ICommand _viewCodeCommand;

        #endregion

        #region Constructors

        public PatternInstanceViewModel(PatternInstance patternInstance, PatternModelViewModel parentPatternModel)
            : base(parentPatternModel, true)
        {
            // set pattern instance
            _patternInstance = patternInstance;

            // set properties command
            _propertiesCommand = new PatternInstancePropertiesCommand(this);

            // set view code command
            _viewCodeCommand = new PatternInstanceViewCodeCommand(this);
        }

        #endregion

        #region Properties

        // name of the pattern instance
        public string PatternInstanceName
        {
            get { return _patternInstance.Name; }
        }

        // pattern instance itself
        public PatternInstance PatternInstance
        {
            get { return _patternInstance; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// get the table of properties of this pattern instance
        /// </summary>
        /// <returns></returns>
        public override DataTable GetProperties()
        {
            // create new data table
            DataTable dtProperties = new DataTable();

            // create property name column
            DataColumn colPropertyName = new DataColumn("Property", typeof(string));
            dtProperties.Columns.Add(colPropertyName);

            // create property value column
            DataColumn colPropertyValue = new DataColumn("Value", typeof(string));
            dtProperties.Columns.Add(colPropertyValue);

            // add instance name row
            DataRow rowInstanceName = dtProperties.NewRow();
            dtProperties.Rows.Add(rowInstanceName);
            rowInstanceName["Property"] = "Instance Name";
            rowInstanceName["Value"] = _patternInstance.Name;

            // add pattern type row
            DataRow rowPatternType = dtProperties.NewRow();
            dtProperties.Rows.Add(rowPatternType);
            rowPatternType["Property"] = "Pattern Type";
            rowPatternType["Value"] = _patternInstance.PatternModel.Name;

            // add SPC File Path row
            DataRow rowSPCFilePath = dtProperties.NewRow();
            dtProperties.Rows.Add(rowSPCFilePath);
            rowSPCFilePath["Property"] = "SPC File Path";
            rowSPCFilePath["Value"] = Service.GetFullPath(_patternInstance.SPCFilePath);

            // add Designer File Path row
            DataRow rowDesignerFilePath = dtProperties.NewRow();
            dtProperties.Rows.Add(rowDesignerFilePath);
            rowDesignerFilePath["Property"] = "Designer File Path";
            rowDesignerFilePath["Value"] = Service.GetFullPath(_patternInstance.DesignerFilePath);

            // add pattern type row
            DataRow rowCodeFilePath = dtProperties.NewRow();
            dtProperties.Rows.Add(rowCodeFilePath);
            rowCodeFilePath["Property"] = "Code File Path";
            rowCodeFilePath["Value"] = Service.GetFullPath(_patternInstance.CodeFilePath);

            // add pattern locations row
            DataRow rowPatternLocations = dtProperties.NewRow();
            dtProperties.Rows.Add(rowPatternLocations);
            rowPatternLocations["Property"] = "Pattern Locations";
            rowPatternLocations["Value"] = "";
            foreach (var location in _patternInstance.PatternLocations)
            {
                rowPatternLocations["Value"] += location.RootPath + ",";
            }
            if (rowPatternLocations["Value"].ToString().EndsWith(","))
            {
                rowPatternLocations["Value"] = rowPatternLocations["Value"].ToString().Substring(0, rowPatternLocations["Value"].ToString().Length - 1);
            }

            // add parent instance row
            DataRow rowParentInstance = dtProperties.NewRow();
            dtProperties.Rows.Add(rowParentInstance);
            rowParentInstance["Property"] = "Instance Derived From";
            rowParentInstance["Value"] = "";

            // add designer comments row
            DataRow rowDesignerComments = dtProperties.NewRow();
            dtProperties.Rows.Add(rowDesignerComments);
            rowDesignerComments["Property"] = "Developer Comments";
            rowDesignerComments["Value"] = "";

            // return the data table of properties
            return dtProperties;
        }

        #endregion

        #region Commands

        #region View Code Command

        /// <summary>
        /// return the command used to show the property of a pattern instance
        /// </summary>
        public ICommand ViewCodeCommand
        {
            get { return _viewCodeCommand; }
        }

        /// <summary>
        /// handle the showing properties of pattern instance
        /// </summary>
        private class PatternInstanceViewCodeCommand : ICommand
        {
            PatternInstanceViewModel _patternInstance;

            public PatternInstanceViewCodeCommand(PatternInstanceViewModel patternInstance)
            {
                _patternInstance = patternInstance;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            event EventHandler ICommand.CanExecuteChanged
            {
                add { }
                remove { }
            }

            public void Execute(object parameter)
            {
                var patternInstance = _patternInstance.PatternInstance;
                Service.OpenFile(patternInstance.SPCFilePath);
            }
        }

        #endregion

        #region Properties Command

        /// <summary>
        /// return the command used to show the property of a pattern instance
        /// </summary>
        public ICommand PropertiesCommand
        {
            get { return _propertiesCommand; }
        }

        /// <summary>
        /// handle the showing properties of pattern instance
        /// </summary>
        private class PatternInstancePropertiesCommand : ICommand
        {
            PatternInstanceViewModel _patternInstance;

            public PatternInstancePropertiesCommand(PatternInstanceViewModel patternInstance)
            {
                _patternInstance = patternInstance;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            event EventHandler ICommand.CanExecuteChanged
            {
                add { }
                remove { }
            }

            public void Execute(object parameter)
            {
                _patternInstance.ShowProperties();
            }
        }

        void ShowProperties()
        {
            System.Windows.MessageBox.Show("Testing");
        }

        #endregion

        #endregion
    }
}
